//========前端验证========================================
//验证手机号
$(".usertel").blur(function () {
    chackTelnoFormat();//js检查
    checkTelnno();//后端数据检查
})

//验证手机前端
function chackTelnoFormat(){
    var username =$(".usertel").val();
    var ele
    var error_tip = $(".error-tel")
    error_tip.html("")
    if(username.trim() ==null || username.trim() ==""){
        ele =" <i></i>\n" +
            "                <span class=\"error-text\">请输入您的手机号</span>"
        error_tip.html(ele)
        error_tip.css("display","block")
        return false;
    }else if(!/^[1][3,4,5,7,8][0-9]{9}$/.test(username)){
        ele =" <i></i>\n" +
            "                <span class=\"error-text\">请输入正确的手机号</span>"
        error_tip.html(ele)
        error_tip.css("display","block")
        return false;
    }else{
        error_tip.html("")
        error_tip.css("display","none")
        return true;
    }
}

//验证密码
$(".userpsd").blur(function () {
    var usepwd =$(".userpsd").val()
    var error_tip = $(".error-psd")
    var ele
    error_tip.html("")
    if(usepwd.trim() ==null || usepwd.trim() ==""){
        ele =" <i></i>\n" +
            "                <span class=\"error-text\">请输入密码</span>"
        error_tip.html(ele)
        error_tip.css("display","block")
        return false;
    }else if(!/^[0-9A-Za-z]{6,12}$/.test(usepwd)){
        ele =" <i></i>\n" +
            "                <span class=\"error-text\">请输入正确的密码</span>"
        error_tip.html(ele)
        error_tip.css("display","block")

    }else{
        error_tip.html("")
        error_tip.css("display","none")
    }
})

//再次确认密码
$(".chack-psd").blur(function () {
    var usepwd =$(".userpsd").val()
    var Cusepwd =$(".chack-psd").val()
    var error_tip = $(".error-chackPsd")
    var ele
    error_tip.html("")
    if(Cusepwd.trim() ==null || Cusepwd.trim() ==""){
        ele =" <i></i>\n" +
            "                <span class=\"error-text\">请输入与上相同的密码</span>"
        error_tip.html(ele)
        error_tip.css("display","block")
        return false;
    }else if(usepwd != Cusepwd){
        ele =" <i></i>\n" +
            "                <span class=\"error-text\">两次输入密码不一致</span>"
        error_tip.html(ele)
        error_tip.css("display","block")
    }else{
        error_tip.html("")
        error_tip.css("display","none")
    }
})

//点击注册
$(".regist-btn").click(function () {
    var username =$(".usertel").val();
    var usepwd =$(".userpsd").val()
    var Cusepwd =$(".chack-psd").val()
    if ((username.trim() ==null || username.trim() =="")&&(usepwd.trim() ==null || usepwd.trim() =="")&&(Cusepwd.trim() ==null || Cusepwd.trim() =="")){
        $(".tip-regist").text("请输入相应的信息")
        $(".hidden").css("display","block")
    }
})


//========后端验证========================================
//验证手机
function checkTelnno() {
    var telno =$(".usertel").val();// 获得手机号
    console.log(telno);
   $.ajax({
       type:"get",
       url:"/fresh/customer/telno/"+telno,
       datatype:"json",
       success:function (result) {
           console.log(result);
           if (result.status == 1){
               //用户已经注册过
               $(".tip-regist").text(result.data)
               $(".hidden").css("display","block")
           }else {
               //尚未注册
               $(".tip-regist").text("")
               $(".hidden").css("display","none")
               console.log("用户尚未注册:  "+result.data);
           }
       }
   })
}

//获取验证码
var interValObj ;// 计时器变量，控制时间
var count = 60; // 间隔函数
var curCount ;//当前剩余秒数

//2. 获得按钮点击，点击后，按钮内部显示 倒计时
$(".code-btn").click(function () {
    curCount = count
    $(".code-btn").attr("disabled","true")
    $(".code-btn").val(curCount+"秒后重新发送")
    $(".code-btn").css("font-size","12px")
    interValObj = window.setInterval(setRemainTime,1000)
})

//计时器
function setRemainTime() {
    if (curCount == 0){
        window.clearInterval(interValObj)
        $(".code-btn").removeAttr("disabled")
        $(".code-btn").val("重新发送验证码")
    }else {
        curCount--
        $(".code-btn").val(curCount + "秒后重新发送");
    }
}

//验证验证码
var serverCode ;//服务器中的而短信验证码
//  获得页面手机号，ajax请求到后端先检查手机号，若没被注册过，获得验证码，服务器返回验证码
$(".code-btn").click(function () {
    var telno =$(".usertel").val();// 获得手机号
    $.ajax({
        type:"post",
        url:"/fresh/customer/code",
        data:"telno="+telno,
        datatype:"json",
        success:function (result) {
            console.log(result)
            //??????????
            if(result.status ==2){//成功调用
                //用户输入验证码
                serverCode = result.data;//后端返回的验证码
            }else{//调用失败
                $(".tip-regist").text(result.data);
                $(".hidden").css("display","block")
                window.clearInterval(interValObj);
                $(".code-btn").removeAttr("disabled");
                $(".code-btn").val("重新发送验证码");
            }
        }
    })
})

//带着验证码与表单中其他数据进行注册
$(".regist-btn").click(function () {
    var inputCode = $(".usercode").val()
    if (inputCode == serverCode) {
        //ajax 请求注册用户 （添加数据到数据库中）
        console.log("ajax 请求注册用户 （添加数据到数据库中）");

        $(".error-code").css("display","none")
        regist();
        /* var isAgree = $(".isAgree")
         if (isAgree.checked != null){
             regist();
         }*/

    }else {
        //用户输入的验证码错误
        var ele =" <i></i>\n" +
            "                <span class=\"error-text\">用户输入的验证码错误</span>"
        $(".error-code").html(ele)
        $(".error-code").css("display","block")
    }
})

//4.注册业务：就是添加功能
function regist() {
    console.log($("#register-form"));
    let formdata = $("#register-form").serialize()
    console.log(formdata);
    $.ajax({
        type:"POST",
        url:"/fresh/customer/add",
        data: formdata,
        datatype:"json",
        success:function (result) {
            console.log("result:"+result);
            if (result.status == 0){
                //添加失败
                console.log("注册失败");
            }else {
                var customerId = result.data.customerId
                console.log("result data:"+result.data);
                console.log("customerId:"+customerId);
                addCart(customerId)

                // window.location.href="/index";
            }
        }
    })
}

//注册后生成一个购物车
function addCart(id) {
    console.log(id);
    $.ajax({
        type:"post",
        url:"/fresh/cart/add",
        data:"custId="+id,
        datatype:"json",
        success:function (result) {
            console.log(result);
            if (result.status==1) {
                console.log(result.data);
                console.log("注册成功");
                window.location.href="/fresh/customer/login.jsp";
            }else
                console.log(result.data);
        }
    })
}
